package com.bitland.demo.api.advice;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * ********************************************************
 *
 * @Author:Joe
 * @Date:Created in 13:46    2020/6/5
 * @Description: 自动填充公共字段处理器
 * <p>
 * ********************************************************
 */
@Component
@Slf4j
public class MetaHandler implements MetaObjectHandler {

    public static final String currentUserId = "秋名山老司机";



    /**
     * @Author:Joe
     * @Date:Created in 13:50  2020/6/5
     * @Description: 新增数据时自动填充
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("createdBy",currentUserId,metaObject);
        this.setFieldValByName("createdDate", LocalDateTime.now(),metaObject);
        this.setFieldValByName("lastModifiedBy",currentUserId,metaObject);
        this.setFieldValByName("lastModifiedDate",LocalDateTime.now(),metaObject);
    }

    /**
     * @Author:Joe
     * @Date:Created in 13:51  2020/6/5
     * @Description: 修改数据时自动填充
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("lastModifiedBy",currentUserId,metaObject);
        this.setFieldValByName("lastModifiedDate",LocalDateTime.now(),metaObject);
    }
}
